home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / SPLITTER.C < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-13  |  14.0 KB  |  505 lines

  1. /*****************************************************************************
  2.   
  3.   Zope Public License (ZPL) Version 1.0
  4.   -------------------------------------
  5.   
  6.   Copyright (c) Digital Creations.  All rights reserved.
  7.   
  8.   This license has been certified as Open Source(tm).
  9.   
  10.   Redistribution and use in source and binary forms, with or without
  11.   modification, are permitted provided that the following conditions are
  12.   met:
  13.   
  14.   1. Redistributions in source code must retain the above copyright
  15.      notice, this list of conditions, and the following disclaimer.
  16.   
  17.   2. Redistributions in binary form must reproduce the above copyright
  18.      notice, this list of conditions, and the following disclaimer in
  19.      the documentation and/or other materials provided with the
  20.      distribution.
  21.   
  22.   3. Digital Creations requests that attribution be given to Zope
  23.      in any manner possible. Zope includes a "Powered by Zope"
  24.      button that is installed by default. While it is not a license
  25.      violation to remove this button, it is requested that the
  26.      attribution remain. A significant investment has been put
  27.      into Zope, and this effort will continue if the Zope community
  28.      continues to grow. This is one way to assure that growth.
  29.   
  30.   4. All advertising materials and documentation mentioning
  31.      features derived from or use of this software must display
  32.      the following acknowledgement:
  33.   
  34.        "This product includes software developed by Digital Creations
  35.        for use in the Z Object Publishing Environment
  36.        (http://www.zope.org/)."
  37.   
  38.      In the event that the product being advertised includes an
  39.      intact Zope distribution (with copyright and license included)
  40.      then this clause is waived.
  41.   
  42.   5. Names associated with Zope or Digital Creations must not be used to
  43.      endorse or promote products derived from this software without
  44.      prior written permission from Digital Creations.
  45.   
  46.   6. Modified redistributions of any form whatsoever must retain
  47.      the following acknowledgment:
  48.   
  49.        "This product includes software developed by Digital Creations
  50.        for use in the Z Object Publishing Environment
  51.        (http://www.zope.org/)."
  52.   
  53.      Intact (re-)distributions of any official Zope release do not
  54.      require an external acknowledgement.
  55.   
  56.   7. Modifications are encouraged but must be packaged separately as
  57.      patches to official Zope releases.  Distributions that do not
  58.      clearly separate the patches from the original work must be clearly
  59.      labeled as unofficial distributions.  Modifications which do not
  60.      carry the name Zope may be packaged in any form, as long as they
  61.      conform to all of the clauses above.
  62.   
  63.   
  64.   Disclaimer
  65.   
  66.     THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  67.     EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  68.     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  69.     PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  70.     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  71.     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  72.     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  73.     USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  74.     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  75.     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  76.     OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  77.     SUCH DAMAGE.
  78.   
  79.   
  80.   This software consists of contributions made by Digital Creations and
  81.   many individuals on behalf of Digital Creations.  Specific
  82.   attributions are listed in the accompanying credits file.
  83.   
  84.  ****************************************************************************/
  85. #include "Python.h"
  86. #include <ctype.h>
  87.  
  88. #define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;}
  89. #define UNLESS(E) if(!(E))
  90. #define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V)
  91.  
  92. static PyObject *next_word();
  93.  
  94. typedef struct 
  95. {
  96.     PyObject_HEAD
  97.     PyObject *text, *synstop;
  98.     char *here, *end;
  99.     int index;
  100. } Splitter;
  101.  
  102. static void
  103. Splitter_reset(Splitter *self)
  104. {
  105.     self->here = PyString_AsString(self->text);
  106.     self->index = -1;
  107. }
  108.  
  109. static void
  110. Splitter_dealloc(Splitter *self) 
  111. {
  112.     Py_XDECREF(self->text);
  113.     Py_XDECREF(self->synstop);
  114.     PyMem_DEL(self);
  115. }
  116.  
  117. static int
  118. Splitter_length(Splitter *self)
  119. {
  120.     PyObject *res=0;
  121.  
  122.     Splitter_reset(self);
  123.     while(1)
  124.       {
  125.     UNLESS_ASSIGN(res,next_word(self,NULL,NULL)) return -1;
  126.     UNLESS(PyString_Check(res))
  127.       {
  128.         Py_DECREF(res);
  129.         break;
  130.       }
  131.       }
  132.     return self->index+1;
  133. }
  134.  
  135. static PyObject *
  136. Splitter_concat(Splitter *self, PyObject *other)
  137. {
  138.     PyErr_SetString(PyExc_TypeError, "Cannot concatenate Splitters.");
  139.     return NULL;
  140. }
  141.  
  142. static PyObject *
  143. Splitter_repeat(Splitter *self, long n)
  144. {
  145.     PyErr_SetString(PyExc_TypeError, "Cannot repeat Splitters.");
  146.     return NULL;
  147. }
  148.  
  149. /*
  150.   Map an input word to an output word by applying standard
  151.   filtering/mapping words, including synonyms/stop words.
  152.  
  153.   Input is a word.
  154.   
  155.   Output is:
  156.  
  157.      None -- The word is a stop word
  158.  
  159.      sometext -- A replacement for the word
  160.  */
  161. static PyObject *
  162. check_synstop(Splitter *self, PyObject *word)
  163. {
  164.     PyObject *value;
  165.     char *cword;
  166.     int len;
  167.     
  168.     cword = PyString_AsString(word);
  169.     len = PyString_Size(word) - 1;
  170.  
  171.     len = PyString_Size(word);
  172.     if(len < 2)    /* Single-letter words are stop words! */
  173.     {
  174.       Py_INCREF(Py_None);
  175.       return Py_None;
  176.     }
  177.  
  178.     /*************************************************************
  179.       Test whether a word has any letters.                       *
  180.                                                                  */    
  181.     for (; --len >= 0 && ! isalpha((unsigned char)cword[len]); );
  182.     if (len < 0)
  183.     {
  184.         Py_INCREF(Py_None);
  185.         return Py_None;
  186.     }
  187.     /*
  188.      * If no letters, treat it as a stop word.
  189.      *************************************************************/
  190.  
  191.     Py_INCREF(word);
  192.  
  193.     if (self->synstop == NULL) return word;
  194.  
  195.     while ((value = PyObject_GetItem(self->synstop, word)) &&
  196.        PyString_Check(value))
  197.     {
  198.         ASSIGN(word,value);
  199.     if(len++ > 100) break;    /* Avoid infinite recurssion */
  200.     }
  201.  
  202.     if (value == NULL)
  203.     {
  204.         PyErr_Clear();
  205.         return word;
  206.     }
  207.  
  208.     return value;        /* Which must be None! */
  209. }
  210.  
  211. #define MAX_WORD 64        /* Words longer than MAX_WORD are stemmed */
  212.    
  213. static PyObject *
  214. next_word(Splitter *self, char **startpos, char **endpos)
  215. {
  216.   char wbuf[MAX_WORD];
  217.   char *end, *here, *b;
  218.   int i = 0, c;
  219.   PyObject *pyword, *res;
  220.  
  221.   here=self->here;
  222.   end=self->end;
  223.   b=wbuf;
  224.   while (here < end)
  225.     {
  226.       /* skip hyphens */ 
  227.       if ((i > 0) && (*here == '-'))
  228.         {
  229.       here++;
  230.       while (isspace(*here) && (here < end)) here++;
  231.       continue;
  232.     }
  233.  
  234.       c=tolower(*here);
  235.       
  236.       /* Check to see if this character is part of a word */
  237.       if(isalnum((unsigned char)c) || c=='/')
  238.         { /* Found a word character */
  239.       if(startpos && i==0) *startpos=here;
  240.       if(i++ < MAX_WORD) *b++ = c;
  241.         }
  242.       else if (i != 0)
  243.         { /* We've found the end of a word */
  244.       if(i >= MAX_WORD) i=MAX_WORD; /* "stem" the long word */
  245.  
  246.       UNLESS(pyword = PyString_FromStringAndSize(wbuf, i))
  247.             {
  248.           self->here=here;
  249.           return NULL;
  250.         }
  251.       
  252.       UNLESS(res = check_synstop(self, pyword))
  253.             {
  254.           self->here=here;
  255.           Py_DECREF(pyword);
  256.           return NULL;
  257.         }
  258.       
  259.       if (res != Py_None)
  260.             {
  261.           if(endpos) *endpos=here;
  262.           self->here=here;
  263.           Py_DECREF(pyword);
  264.           self->index++;
  265.           return res;
  266.         }
  267.  
  268.       /* The word is a stopword, so ignore it */ 
  269.  
  270.       Py_DECREF(res);          
  271.       Py_DECREF(pyword);
  272.       i = 0;
  273.       b=wbuf;
  274.         }
  275.       
  276.       here++;
  277.     }
  278.  
  279.   self->here=here;
  280.  
  281.   /* We've reached the end of the string */
  282.  
  283.   if(i >= MAX_WORD) i=MAX_WORD; /* "stem" the long word */
  284.   if (i == 0)
  285.     { 
  286.       /* No words */
  287.       self->here=here;
  288.       Py_INCREF(Py_None);
  289.       return Py_None;
  290.     }
  291.   
  292.   UNLESS(pyword = PyString_FromStringAndSize(wbuf, i)) return NULL;
  293.   
  294.   if(endpos) *endpos=here;
  295.   res = check_synstop(self, pyword);
  296.   Py_DECREF(pyword);
  297.   if(PyString_Check(res)) self->index++;
  298.   return res;
  299. }
  300.  
  301. static PyObject *
  302. Splitter_item(Splitter *self, int i)
  303. {
  304.     PyObject *word = NULL;
  305.  
  306.     if (i <= self->index) Splitter_reset(self);
  307.  
  308.     while(self->index < i)
  309.     {
  310.         Py_XDECREF(word);
  311.  
  312.         UNLESS(word = next_word(self,NULL,NULL)) return NULL; 
  313.         if (word == Py_None)
  314.         {
  315.             Py_DECREF(word);
  316.             PyErr_SetString(PyExc_IndexError,
  317.                 "Splitter index out of range");
  318.             return NULL;
  319.         }
  320.     }
  321.  
  322.     return word;
  323. }
  324.  
  325. static PyObject *
  326. Splitter_slice(Splitter *self, int i, int j)
  327. {
  328.     PyErr_SetString(PyExc_TypeError, "Cannot slice Splitters.");
  329.     return NULL;
  330. }
  331.  
  332. static PySequenceMethods Splitter_as_sequence = {
  333.     (inquiry)Splitter_length,        /*sq_length*/
  334.     (binaryfunc)Splitter_concat,     /*sq_concat*/
  335.     (intargfunc)Splitter_repeat,     /*sq_repeat*/
  336.     (intargfunc)Splitter_item,       /*sq_item*/
  337.     (intintargfunc)Splitter_slice,   /*sq_slice*/
  338.     (intobjargproc)0,                    /*sq_ass_item*/
  339.     (intintobjargproc)0,                 /*sq_ass_slice*/
  340. };
  341.  
  342. static PyObject *
  343. Splitter_pos(Splitter *self, PyObject *args)
  344. {
  345.     char *start, *end, *ctext;
  346.     PyObject *res;
  347.     int i;
  348.  
  349.     UNLESS(PyArg_Parse(args, "i", &i)) return NULL;
  350.  
  351.     if (i <= self->index) Splitter_reset(self);
  352.  
  353.     while(self->index < i)
  354.     {
  355.     UNLESS(res=next_word(self, &start, &end)) return NULL;
  356.     if(PyString_Check(res))
  357.       {
  358.             self->index++;
  359.         Py_DECREF(res);
  360.         continue;
  361.       }
  362.     Py_DECREF(res);
  363.     PyErr_SetString(PyExc_IndexError, "Splitter index out of range");
  364.     return NULL;
  365.     }
  366.  
  367.     ctext=PyString_AsString(self->text);
  368.     return Py_BuildValue("(ii)", start - ctext, end - ctext);
  369. }
  370.  
  371. static PyObject *
  372. Splitter_indexes(Splitter *self, PyObject *args)
  373. {
  374.   PyObject *word, *r, *w=0, *index=0;
  375.   int i=0;
  376.  
  377.   UNLESS(PyArg_ParseTuple(args,"O",&word)) return NULL;
  378.   UNLESS(r=PyList_New(0)) return NULL;
  379.   UNLESS(word=check_synstop(self, word)) goto err;
  380.  
  381.   Splitter_reset(self);
  382.   while(1)
  383.     {
  384.       UNLESS_ASSIGN(w,next_word(self, NULL, NULL)) goto err;
  385.       UNLESS(PyString_Check(w)) break;
  386.       if(PyObject_Compare(word,w)==0)
  387.     {
  388.       UNLESS_ASSIGN(index,PyInt_FromLong(i)) goto err;
  389.       if(PyList_Append(r,index) < 0) goto err;
  390.     }
  391.       i++;
  392.     }
  393.   Py_XDECREF(w);
  394.   Py_XDECREF(index);
  395.   return r;
  396.  
  397. err:
  398.   Py_DECREF(r);
  399.   Py_XDECREF(index);
  400.   return NULL;
  401. }
  402.  
  403. static struct PyMethodDef Splitter_methods[] = {
  404.     { "pos", (PyCFunction)Splitter_pos, 0,
  405.       "pos(index) -- Return the starting and ending position of a token" },
  406.     { "indexes", (PyCFunction)Splitter_indexes, METH_VARARGS,
  407.       "indexes(word) -- Return al list of the indexes of word in the sequence",
  408.     },
  409.     { NULL, NULL }        /* sentinel */
  410. };
  411.  
  412. static PyObject *
  413. Splitter_getattr(Splitter *self, char *name) 
  414. {
  415.     return Py_FindMethod(Splitter_methods, (PyObject *)self, name);
  416. }
  417.  
  418. static char SplitterType__doc__[] = "";
  419.  
  420. static PyTypeObject SplitterType = {
  421.     PyObject_HEAD_INIT(NULL)
  422.     0,                                 /*ob_size*/
  423.     "Splitter",                    /*tp_name*/
  424.     sizeof(Splitter),              /*tp_basicsize*/
  425.     0,                                 /*tp_itemsize*/
  426.     /* methods */
  427.     (destructor)Splitter_dealloc,  /*tp_dealloc*/
  428.     (printfunc)0,                      /*tp_print*/
  429.     (getattrfunc)Splitter_getattr, /*tp_getattr*/
  430.     (setattrfunc)0,                    /*tp_setattr*/
  431.     (cmpfunc)0,                        /*tp_compare*/
  432.     (reprfunc)0,                       /*tp_repr*/
  433.     0,                                 /*tp_as_number*/
  434.     &Splitter_as_sequence,         /*tp_as_sequence*/
  435.     0,                                 /*tp_as_mapping*/
  436.     (hashfunc)0,                       /*tp_hash*/
  437.     (ternaryfunc)0,                    /*tp_call*/
  438.     (reprfunc)0,                       /*tp_str*/
  439.  
  440.     /* Space for future expansion */
  441.     0L,0L,0L,0L,
  442.     SplitterType__doc__ /* Documentation string */
  443. };
  444.  
  445. static PyObject *
  446. get_Splitter(PyObject *modinfo, PyObject *args)
  447. {
  448.     Splitter *self;
  449.     PyObject *doc, *synstop = NULL;
  450.  
  451.     UNLESS(PyArg_ParseTuple(args,"O|O",&doc,&synstop)) return NULL;
  452.  
  453.     UNLESS(self = PyObject_NEW(Splitter, &SplitterType)) return NULL;
  454.  
  455.     if(synstop)
  456.       {
  457.     self->synstop=synstop;
  458.     Py_INCREF(synstop);
  459.       }
  460.     else self->synstop=NULL;
  461.  
  462.     UNLESS(self->text = PyObject_Str(doc)) goto err;
  463.     UNLESS(self->here=PyString_AsString(self->text)) goto err;
  464.     self->end = self->here + PyString_Size(self->text);
  465.     self->index = -1;
  466.     return (PyObject*)self;
  467. err:
  468.     Py_DECREF(self);
  469.     return NULL;
  470. }
  471.  
  472. static struct PyMethodDef Splitter_module_methods[] = {
  473.     { "Splitter", (PyCFunction)get_Splitter, METH_VARARGS,
  474.       "Splitter(doc[,synstop]) -- Return a word splitter" },
  475.     { NULL, NULL }
  476. };
  477.  
  478. static char Splitter_module_documentation[] = 
  479. "Parse source strings into sequences of words\n"
  480. "\n"
  481. "for use in an inverted index\n"
  482. "\n"
  483. "$Id: Splitter.c,v 1.12.36.1 2000/09/13 14:53:30 brian Exp $\n"
  484. ;
  485.  
  486.  
  487. void
  488. initSplitter() 
  489. {
  490.   PyObject *m, *d;
  491.   char *rev="$Revision: 1.12.36.1 $";
  492.   
  493.   /* Create the module and add the functions */
  494.   m = Py_InitModule4("Splitter", Splitter_module_methods,
  495.                      Splitter_module_documentation,
  496.                      (PyObject*)NULL,PYTHON_API_VERSION);
  497.   
  498.   /* Add some symbolic constants to the module */
  499.   d = PyModule_GetDict(m);
  500.   PyDict_SetItemString(d, "__version__",
  501.                PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));
  502.  
  503.   if (PyErr_Occurred()) Py_FatalError("can't initialize module Splitter");
  504. }
  505.